package com.zy.service.impl;

import cn.hutool.core.lang.UUID;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.zy.ai.service.AiVectorService;
import com.zy.entity.AiQuestion;
import com.zy.mapper.AiQuestionMapper;
import com.zy.service.IAiQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @BelongsProject: SmartBidAI
 * @BelongsPackage: com.zy.service.impl
 * @Author: zhangyan
 * @CreateTime: 2025-09-05  18:41
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class AiQuestionServiceImpl extends ServiceImpl<AiQuestionMapper, AiQuestion> implements IAiQuestionService {
    @Autowired
    private AiQuestionMapper AiQuestionMapper;

    @Autowired
    private AiVectorService aiVectorService;

    private static final String COLLECTION = "ai_vectors";

    /**
     * 新问题记录
     */
    @Override
    public void saveQuestionEmbedding(String question,String answer, List<Double> embedding) {
        String uuId = UUID.fastUUID().toString();
        // 1️⃣ 存数据库
        AiQuestion entity = new AiQuestion();
        entity.setQuestionText(question);
        entity.setAnswerText(answer);
        entity.setEmbeddingId(uuId);
        this.save(entity);

        // 2️⃣ 存向量数据库
        aiVectorService.saveQuestionEmbedding(COLLECTION, question, answer,uuId);
    }

    /**
     * 查询相似问题
     */
    @Override
    public List<?> searchSimilarQuestions(List<Double> embedding, int topK) {
        return aiVectorService.searchSimilar(COLLECTION, embedding, topK);
    }
}
